﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin
{
    public partial class Manual_adjust : Form
    {
        List<Stu_group> all_group;//全部组
        List<Teacher> all_teacher;//全部老师
        List<Student> all_students;//全部学生
        List<t_data> all_tdata;//全部老师选择数据
        List<g_data> all_gdata;//全部组选择数据
        List<Stu_group> t_groups;//指定老师选择的所有组

        public Manual_adjust()
        {
            InitializeComponent();
        }

        private void Manul_adjust_Load(object sender, EventArgs e)//显示所有：组、老师。准备所有：学生、组选择数据、老师选择数据。
        {
            toolTip1.SetToolTip(uiButton1, "选择该组作为该教师的小组");
            toolTip1.SetToolTip(uiButton3, "点击保存你的所有更改");
            BLL.B_Group b_Group = new BLL.B_Group();
            all_group = b_Group.getList();

            BLL.B_Teacher b_Teacher = new BLL.B_Teacher();
            all_teacher = b_Teacher.GetList();

            BLL.B_Student b_Student = new BLL.B_Student();
            all_students = b_Student.GetList();

            BLL.B_T b_T = new BLL.B_T();
            all_tdata = b_T.getList();

            BLL.B_G b_G = new BLL.B_G();
            all_gdata = b_G.getList();

            //if(all_gdata.Count != all_group.Count)
            //    return;

            all_group=all_group.OrderBy(g => g.group_id).ToList();
            Groups_view.DataSource = all_group;
            Teachers_view.DataSource = all_teacher;
            Teachers_view.Columns["pwd"].Visible = false;
            Teachers_view.Columns["teacher_uname"].Visible = false;
        }

        private void Groups_view_MouseClick(object sender, MouseEventArgs e)
        {
            if (Groups_view.CurrentCell == null) return;

            int idx = Groups_view.CurrentCell.RowIndex;
            Disp_Group_info(idx, all_group);
        }

        private void Teachers_view_MouseClick(object sender, MouseEventArgs e)
        {
            teach_Group.DataSource = null;
            Disp_teachers_group();
        }

        private void Disp_teachers_group()
        {
            if(Teachers_view.CurrentCell == null) return;

            int idx = Teachers_view.CurrentCell.RowIndex;

            var t_select = all_tdata.Where(ts => ts.t_id == all_teacher[idx].teacher_id).ToList();
            if (t_select.Count == 0) return;

            List<int> g_ids = new List<int>();

            for (int i = 1; i <= 10; i++)
            {
                var g = get_Choice(t_select[0], $"g{i}");
                if(g!=-1) g_ids.Add(g);
            }


            //if (t_select[0].g1 != -1) g_ids.Add((int)t_select[0].g1);
            //if (t_select[0].g2 != -1) g_ids.Add((int)t_select[0].g2);
            //if (t_select[0].g3 != -1) g_ids.Add((int)t_select[0].g3);
            //if (t_select[0].g4 != -1) g_ids.Add((int)t_select[0].g4);
            //if (t_select[0].g5 != -1) g_ids.Add((int)t_select[0].g5);
            //if (t_select[0].g6 != -1) g_ids.Add((int)t_select[0].g6);
            //if (t_select[0].g7 != -1) g_ids.Add((int)t_select[0].g7);
            //if (t_select[0].g8 != -1) g_ids.Add((int)t_select[0].g8);
            //if (t_select[0].g9 != -1) g_ids.Add((int)t_select[0].g9);
            //if (t_select[0].g10 != -1) g_ids.Add((int)t_select[0].g10);


            t_groups = all_group.Where(g => g_ids.Contains(g.group_id)).ToList();
            teach_Group.DataSource = t_groups;
        }
        private int get_Choice(object Data, string pro_name)
        {
            var tChoice = Data.GetType().GetProperty(pro_name).GetValue(Data, null);
            return Object.ReferenceEquals(tChoice,null) ? -1 : (int)tChoice;
        }

        private void set_Choice(object Data, string pro_name, int g_id)
        {
            Data.GetType().GetProperty(pro_name).SetValue(Data, g_id);
        }
            
        //修改老师、学生组配对
        private void modify_pair_Click(object sender, EventArgs e)
        {
            if(Groups_view.CurrentCell==null) return;

            int dgview_idx = Groups_view.CurrentCell.RowIndex;
            var g_id = all_group[dgview_idx].group_id; //网格中选中的行对应组的id

            var g_datas = all_gdata.Where(g=>g.g_id == g_id).ToList();
            if (g_datas.Count == 0) return;
            var g_data = g_datas[0];

            int orgi_teacher = g_data.t_id.HasValue ? (int)g_data.t_id:-1; //保存选中组原有的指导老师，这里指定另一个老师，原老师需要减去该组

            int tea_idx = Teachers_view.CurrentCell.RowIndex;
            var teacher_id = all_teacher[tea_idx].teacher_id;//现在选中的老师id

            var teacher_group = all_tdata.Where(a_t => a_t.t_id == teacher_id).ToList();//对应老师在t_data中的记录

            if (teacher_group.Count == 0) //如果这个老师还没选组，在t_data中没有记录，则新建一条记录
            {
                Models.t_data t1 = new t_data();
                t1.t_id = teacher_id;
                t1.g1 = g_id;//把组分配给这新老师
                t1.res_count = 1;
                all_tdata.Add(t1); //在t_data中添加一条新纪录
                g_data.t_id = teacher_id; //在g_data表中，修改这组的指导老师
                modi_orgTeacher(orgi_teacher, g_id);//删除原来这组的指导老师
            }
            else if (teacher_group[0].res_count<log_info.Sys_Config.tearch_choice)//如果这个老师选了组，还没有达到上限
            {
                for(int i = 1;i<=10;i++)
                {
                    var g = get_Choice(teacher_group[0], $"g{i}");
                    if(g == -1)
                    {
                        set_Choice(teacher_group[0], $"g{i}", g_id);
                        teacher_group[0].res_count++;
                        break;
                    }
                }

                g_data.t_id = teacher_id; //在g_data表中，修改这组的指导老师
                modi_orgTeacher(orgi_teacher, g_id);//删除原来这组的指导老师
                all_group[dgview_idx].teacher_id = teacher_id;
                all_group[dgview_idx].teacher_name = all_teacher[tea_idx].teacher_name;
            }
            else
            {
                MessageBox.Show("教师指导队伍，已经超过总数");
                return ;
            }

            Disp_teachers_group();
        }

        void modi_orgTeacher(int tid, int gid)
        {
            if (tid == -1) return;

            var teacher = all_tdata.Where(a_t=>a_t.t_id==tid).FirstOrDefault();
            for (int i = 1; i <= 10; i++)
            {
                var g = get_Choice(teacher, $"g{i}");
                if (g == gid)
                {
                    set_Choice(teacher, $"g{i}", -1);
                    teacher.res_count--;
                    break;
                }
            }
        }

        private void teach_Group_MouseClick(object sender, MouseEventArgs e)
        {
            if(teach_Group.CurrentCell == null) return;

            int idx = teach_Group.CurrentCell.RowIndex;
            Disp_Group_info(idx, t_groups);
        }

        private void Disp_Group_info(int idx, List<Stu_group> stu_Groups)
        {
            Group_info.Text = stu_Groups[idx].project_info;
            Group_name.Text = stu_Groups[idx].group_name;
            Project_name.Text = stu_Groups[idx].project_name;

            var g_member = all_students.Where<Student>(stu => stu.group_id == stu_Groups[idx].group_id).ToList();
            Group_member.DataSource = g_member;
            Group_member.Columns["pwd"].Visible = false;
        }

        private void save_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.B_Manul_adjust b_Manul_Adjust = new BLL.B_Manul_adjust();
                if (b_Manul_Adjust.adjust(all_tdata, all_gdata, all_group))
                    MessageBox.Show("ok!");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.B_Manul_adjust b_Manul_Adjust = new BLL.B_Manul_adjust();
                if (b_Manul_Adjust.adjust(all_tdata, all_gdata, all_group))
                    MessageBox.Show("ok!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (Groups_view.CurrentCell == null) return;

            int dgview_idx = Groups_view.CurrentCell.RowIndex;
            var g_id = all_group[dgview_idx].group_id; //网格中选中的行对应组的id

            var g_datas = all_gdata.Where(g => g.g_id == g_id).ToList();
            if (g_datas.Count == 0) return;
            var g_data = g_datas[0];

            int orgi_teacher = g_data.t_id.HasValue ? (int)g_data.t_id : -1; //保存选中组原有的指导老师，这里指定另一个老师，原老师需要减去该组

            int tea_idx = Teachers_view.CurrentCell.RowIndex;
            var teacher_id = all_teacher[tea_idx].teacher_id;//现在选中的老师id

            var teacher_group = all_tdata.Where(a_t => a_t.t_id == teacher_id).ToList();//对应老师在t_data中的记录

            if (teacher_group.Count == 0) //如果这个老师还没选组，在t_data中没有记录，则新建一条记录
            {
                Models.t_data t1 = new t_data();
                t1.t_id = teacher_id;
                t1.g1 = g_id;//把组分配给这新老师
                t1.res_count = 1;
                all_tdata.Add(t1); //在t_data中添加一条新纪录
                g_data.t_id = teacher_id; //在g_data表中，修改这组的指导老师
                modi_orgTeacher(orgi_teacher, g_id);//删除原来这组的指导老师
            }
            else if (teacher_group[0].res_count < log_info.Sys_Config.tearch_choice)//如果这个老师选了组，还没有达到上限
            {
                for (int i = 1; i <= 10; i++)
                {
                    var g = get_Choice(teacher_group[0], $"g{i}");
                    if (g == -1)
                    {
                        set_Choice(teacher_group[0], $"g{i}", g_id);
                        teacher_group[0].res_count++;
                        break;
                    }
                }

                g_data.t_id = teacher_id; //在g_data表中，修改这组的指导老师
                modi_orgTeacher(orgi_teacher, g_id);//删除原来这组的指导老师
                all_group[dgview_idx].teacher_id = teacher_id;
                all_group[dgview_idx].teacher_name = all_teacher[tea_idx].teacher_name;
            }
            else
            {
                MessageBox.Show("教师指导队伍，已经超过总数");
                return;
            }

            Disp_teachers_group();
        }
    }
}
